<?php

namespace App\Models\CacheModel;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class AppMonthFortuneVipUnlockRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_month_fortune_vip_unlock_record';

    protected $cacheModel = 'fortune.fortune_month';

    /**
     * vip 月运重新解锁权益 是否已使用
     * @param int $uid
     * @return bool
     * @throws \Exception
     */
    public function monthVipReunlockIsUsed(int $uid): bool
    {
        $status = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('month_vip_reunlock_is_used', "{$uid}")
            ->setCacheType('string')->getDataAndSet(function () use ($uid)
            {
                try
                {
                    // type     1=>会员权益解锁 2=>会员权益免费重新计算
                    $num = $this->where(['uid' => $uid, 'type' => 2])->count();

                    return $num > 0 ? 1 : 2;    // 1已使用 2未使用
                }
                catch (\Exception $e)
                {
                    \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
                    return 2;
                }
            }, [], 'month_vip_reunlock_is_used');

        return (int)$status === 1 ? true : false;
    }

    /**
     * 本月 vip 解锁权益 是否已使用
     * @param int $uid
     * @return bool
     * @throws \Exception
     */
    public function currentMonthVipUnlockIsUsed(int $uid): bool
    {
        $cuttentMonth = (int)date('Ym');
        $status = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('current_month_vip_unlock_is_used', "{$uid}:{$cuttentMonth}")
            ->setCacheType('string')->getDataAndSet(function () use ($uid, $cuttentMonth)
            {
                try
                {
                    // type     1=>会员权益解锁 2=>会员权益免费重新计算
                    $num = $this->where(['uid' => $uid, 'type' => 1, 'month' => $cuttentMonth])->count();

                    return $num > 0 ? 1 : 2;    // 1已使用 2未使用
                }
                catch (\Exception $e)
                {
                    \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
                    return 2;
                }
            }, [], 'current_month_vip_unlock_is_used');

        return (int)$status === 1 ? true : false;
    }

    /**
     * 添加 会员权益 解锁月运记录
     * @param array $data
     * @return bool
     */
    public function SaveData(array $data)
    {
        try
        {
            // $data = [    data数据结构
            //     'uid' => '',
            //     'type' => '',    1=>会员权益解锁 2=>会员权益免费重新计算
            //     'rid' => '',
            //     'month' => '202402',
            //     'create_time' => '',
            // ];
            $this->insert($data);

            if ($data['type'] === 1)
            {
                CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('current_month_vip_unlock_is_used', "{$data['uid']}:{$data['month']}")->rmCache('current_month_vip_unlock_is_used');
            } else
            {
                CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('month_vip_reunlock_is_used', $data['uid'])->rmCache('month_vip_reunlock_is_used');
            }
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }
}
